<?php

namespace app\controller;

use app\BaseController;
use app\controller\Post;
use app\controller\Sign;
use app\model\Orders;

class Order extends BaseController
{
    //获取商品列表
    public function getGoodsList()
    {
        $page = input('page', 0);
        $limit = input('limit', 20);
        $sort = input('sort', 'add_time');
        $sort_type = input('sort_type', 'desc');
        $url = 'terminal/api.goods/getGoodsList';
        $time = time();
        $data = [
            "time" => $time,
            "page" => $page,
            "page_size" => $limit,
            "sort" => $sort,
            "sort_type" => $sort_type
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //获取商品详情
    public function getGoodsInfo()
    {
        $sku = input('sku', 0);
        $library = input('library', 0);
        $queryExts = input('queryExts', '');
        $url = 'terminal/api.goods/getGoodsInfo';
        $time = time();
        $data = [
            "time" => $time,
            "sku" => $sku,
            "library" => $library,
            "queryExts" => $queryExts
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, $data['msg']);
    }
    //运费查询
    public function getFreight()
    {
        $primary_address = input('primary_address', ''); //省市县/区
        $address = input('address', ''); //详细地址
        $goods_list = input('goods_list', '');
        $url = 'terminal/api.order/getFreight';
        $time = time();
        $goods_list = json_decode($goods_list, true);
        $data = [
            "time" => $time,
            "primary_address" => $primary_address,
            "address" => $address,
            "goods_list" => $goods_list
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //可售性检查
    public function checkBuy()
    {
        $primary_address = input('primary_address', ''); //省市县/区
        $address = input('address', ''); //详细地址
        $goods_list = input('goods_list', '');
        $url = 'terminal/api.order/checkBuy';
        $time = time();
        $goods_list = json_decode($goods_list, true);
        $data = [
            "time" => $time,
            "primary_address" => $primary_address,
            "address" => $address,
            "goods_list" => $goods_list
        ];
        // $result = Post::params($url, $data);
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //下单
    public function placeOrder()
    {
        $primary_address = input('primary_address', ''); //省市县/区
        $address = input('address', ''); //详细地址
        $goods_list = input('goods_list', '');
        $consignee = input('consignee', '');
        $mobile = input('mobile', '');
        $remark = input('remark', '');
        $url = 'terminal/api.order/placeOrder';
        $third_sn = Date('YmdHis').rand(00000,99999);
        $time = time();
        $goods_list = json_decode($goods_list, true);
        $data = [
            "time" => $time,
            "primary_address" => $primary_address,
            "address" => $address,
            "goods_list" => $goods_list,
            "third_sn" => $third_sn, //商户方订单编号
            "consignee" => $consignee, //收货人
            "mobile" => $mobile, //电话
            "remark" => $remark, //备注,
            "create_time" => date('Y-m-d H:i:s',$time)
        ];
        Orders::createOrder($data);
        [$bool, $data] = Post::params($url, $data);
        Orders::updateOrderNo($third_sn,$data['order_sn']);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //订单查询
    public function queryOrder()
    {
        $order_sn = input('order_sn', '');
        $third_sn = input('third_sn', '');
        $url = 'terminal/api.order/queryOrder';
        $time = time();
        $data = [
            "time" => $time,
            "order_sn" => $order_sn //平台订单编码 与 商户订单编码 二传一
//            "third_sn" => $third_sn, //商户方订单编号
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //取消订单
    public function cancelOrder()
    {
        $order_sn = input('order_sn', '');
        $third_sn = input('third_sn', '');
        $remark = input('remark', '');
        $url = 'terminal/api.order/cancelOrder';
        $time = time();
        $data = [
            "time" => $time,
            "order_sn" => $order_sn, //平台订单编码 与 商户订单编码 二传一
            //"third_sn" => $third_sn, //商户方订单编号 可选
            "remark" => $remark, //取消原因 可选
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //消息列表
    // 消息编码type	说明	json_data的参数说明
    // 1	商品上下架	sku：商品sku编码；library：商品库；state：上下架状态(0:下架;1:上架)
    // 2	商品添加或删除	sku：商品sku编码；library：商品库；ope：操作类型(add:添加;del:删除)
    // 3	商品基础信息变更	sku：商品sku编码；library：商品库；
    // 4	商品价格变动	sku：商品sku编码；library：商品库；
    // 5	订单取消成功	
    // 6	下单成功	
    // 7	订单发货-物流更新	order_sn：平台订单编码；third_sn：商户订单编码；order_goods_ids：订单商品id组合；shipping_list：物流列表（shipping_name：物流名称；shipping_number：物流单号）
    // 8	订单完成	order_sn：平台订单编码；third_sn：商户订单编码；order_state：订单状态(6已完成)
    public function getMsgList()
    {
        $type_list = input('type_list');
        $typeList = explode(',', $type_list);
        $url = 'terminal/api.message/getMsgList';
        $time = time();
        $data = [
            "time" => $time,
            "type_list" => $typeList
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
    //消息删除
    public function delMsg()
    {
        $msg_list = input('msg_list');
        $msg_list = explode(',', $msg_list);
        $url = 'terminal/api.message/delMsg';
        $time = time();
        $data = [
            "time" => $time,
            "msg_list" =>$msg_list
        ];
        [$bool, $data] = Post::params($url, $data);
        return $bool ? $this->returnJson(200, $data) :  $this->returnJson(404, $data, '失败');
    }
}
